﻿@{
    ViewBag.Title = "上传代码";
}

@section scripts
{
    <link rel="stylesheet" href="../Scripts/kindeditor-4.1.10/themes/default/default.css"/>
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/kindeditor-min.js"></script>
    <script charset="utf-8" src="../Scripts/kindeditor-4.1.10/lang/zh_CN.js"></script>
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}

<form method="post" style="height: 100%">
    <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    @*<a class="mini-button" name="SaveInfo" iconCls="icon-save" onclick="SaveInfo" plain="true">保存</a>*@
                    <a class="mini-button" id="codeSubmit" name="codeSubmit" iconCls="icon-edit" onclick="codeSubmit" plain="true">上传代码</a>
                    <a class="mini-button" id="experience" name="experience" iconCls="icon-edit" onclick="experience" plain="true">获取体验二维码</a>
                    <a class="mini-button" id="submitAuth" name="submitAuth" iconCls="icon-edit" onclick="submitAuth" plain="true">提交审核</a>
                    <a class="mini-button" id="auditStatus" name="auditStatus" iconCls="icon-edit" onclick="auditStatus" plain="true">更新审核状态</a>
                    <a class="mini-button" id="release" name="release" iconCls="icon-edit" onclick="release" plain="true">发布代码</a>
                    <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div name="Base" style="height: 98%; overflow: hidden; margin-top: 15px; margin-left: 20px">
            <div id="datagrid1" class="mini-datagrid" style="width: 100%;height: 100%;"></div>
        </div>
    </div>
</form>
<div id="goAuth"></div>
<div id="win1" class="mini-window" title="上传代码" style="width: 300px; height: 250px; display: none"
     showMaxButton="true" showCollapseButton="true" showShadow="true"
     showToolbar="true" showFooter="true" showModal="false" allowResize="true" allowDrag="true">
    <div property="toolbar" style="padding: 5px;">
    </div>
    <div property="footer" style="text-align: right; padding: 5px; padding-right: 15px;">
    </div>
    <form id="form1">
        <input id="appid" name="appid" class="mini-hidden"/>
        <table>
            <tr>
                <td>
                    <label for="templateId">模板id：</label>
                </td>
                <td>
                    <input id="templateId" name="templateId" width="200" class="mini-textbox" required="true"/>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="userVersion">版本号：</label>
                </td>
                <td>
                    <input id="userVersion" name="userVersion" width="200" class="mini-textbox" required="true"/>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="userDesc">版本描述：</label>
                </td>
                <td>
                    <input id="userDesc" name="userDesc" width="200" height="80" class="mini-textarea" required="true"/>
                </td>
            </tr>
        </table>
        <div style="padding: 10px; text-align: center">
            <a class="mini-button mini-button-success" onclick="uploadCode">上传代码</a>
        </div>

    </form>
</div>
<div id="win2" class="mini-window" title="上传代码" style="width: 380px; height: 380px; display: none"
     showMaxButton="true" showCollapseButton="true" showShadow="true"
     showToolbar="true" showFooter="true" showModal="false" allowResize="true" allowDrag="true">
    <div property="toolbar" style="padding: 5px;">
    </div>
    <div property="footer" style="text-align: right; padding: 5px; padding-right: 15px;">
    </div>

    <div style="text-align: center;"><img id="experienceQrCoce" width="300"/></div>

</div>
<script>
    mini.parse();

    function FormShown() {
        mini.parse();
    }

    var grid = mini.get("datagrid1");
    grid.set({
        url: "/SmallProgramSetting/GetAppCodeVersions",
        showMaxButton: true,
        showCollapseButton: true,
        showShadow: true,
        showToolbar: true,
        showFooter: true,
        showModal: false,
        columns: [
            { type: "checkcolumn" },
            { field: "OpenAppId", width: 80, headerAlign: "center", header: "小程序appid" },
            { field: "AuthAppId", width: 80, headerAlign: "center", header: "小程序AuthAppId" },
            { field: "CreateTime", width: 80, headerAlign: "center", header: "上传时间", dateFormat: "yyyy-MM-dd HH:mm:ss" },
            { field: "TemplateId", width: 30, headerAlign: "center", header: "模板id" },
            { field: "UserDesc", width: 200, headerAlign: "center", header: "版本描述" },
            { field: "UserVersion", width: 50, headerAlign: "center", header: "版本号" },
            { field: "CodeStatus", width: 50, headerAlign: "center", header: "审核状态" }
        ]
    });
    grid.load();

    function codeSubmit() {
        var data = grid.getSelected();
        if (data == undefined || data == "") {
            alert("请先选择一条数据!");
            return;
        }

        mini.get("appid").setValue(data.AuthAppId);
        mini.get("win1").show();
        mini.get("win1").showAtEl(document.getElementById("goAuth"));
    }

    function uploadCode() {
        var form = new mini.Form("#form1");

        form.validate();
        if (form.isValid() == false) return;
        var param = {
            appid: mini.get("appid").getValue(),
            templateId: mini.get("templateId").getValue(),
            userVersion: mini.get("userVersion").getValue(),
            userDesc: mini.get("userDesc").getValue()
        }

        $.ajax({
            url: "/SmallProgramSetting/CodeCommit",
            data: param,
            type: "post",
            async: false,
            success: function(res) {
                res = JSON.parse(res);
                if (res.status == 1) {
                    alert("上传成功!");
                    mini.get("win1").hide();
                }
            }
        });
    }

    function experience() {
        var data = grid.getSelected();
        if (data == undefined || data == "") {
            alert("请先选择一条数据!");
            return;
        }
        var param = {
            appid: data.AuthAppId
        }

        $.ajax({
            url: "/SmallProgramSetting/GetExperienceQrCoce",
            data: param,
            type: "post",
            async: false,
            success: function(res) {
                res = JSON.parse(res);
                if (res.status == 1) {
                    mini.get("win2").show();
                    $("#experienceQrCoce").attr("src", "data:image/png;base64," + res.img);
                }
            }
        });
    }

    function submitAuth() {
        var data = grid.getSelected();
        if (data == undefined || data == "") {
            alert("请先选择一条数据!");
            return;
        }
        var r = confirm("确认提交审核!");
        if (r == true) {
            var param = {
                appid: data.AuthAppId
            }
            $.ajax({
                url: "/SmallProgramSetting/SubmitAuth",
                data: param,
                type: "post",
                async: false,
                success: function(res) {
                    res = JSON.parse(res);
                    if (res.status == 1) {
                        alert("提交审核成功!");
                    }
                }
            });
        }
    }

    function release() {
        var data = grid.getSelected();
        if (data == undefined || data == "") {
            alert("请先选择一条数据!");
            return;
        }
        var r = confirm("确认发布代码!");
        if (r == true) {
            var param = {
                appid: data.AuthAppId
            }
            $.ajax({
                url: "/SmallProgramSetting/SubmitAuth",
                data: param,
                type: "post",
                async: false,
                success: function(res) {
                    res = JSON.parse(res);
                    if (res.status == 1) {
                        alert("发布成功!");
                    }
                }
            });
        }
    }

    function auditStatus() {
        var data = grid.getSelected();
        if (data == undefined || data == "") {
            alert("请先选择一条数据!");
            return;
        }
         
        var auditId = data.AuditId;
        if (auditId == null || auditId == undefined || auditId == "") {
            alert("该条数据未提交审核!");
            return;
        }

        var param = {
            appid: data.AuthAppId,
            auditId: auditId
        }
        $.ajax({
            url: "/SmallProgramSetting/GetAuditStatus",
            data: param,
            type: "post",
            async: false,
            success: function (res) {
                res = JSON.parse(res);
                if (res.status == 1) {
                    grid.load();
                }
            }
        });
    }

</script>